package com.rimi.project.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/CookieServlet")
public class CookieServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		
		// 读取当前请求中所有的cookie
		Cookie[] cookies = request.getCookies();
		
		if (null != cookies) {
			for (Cookie cookie : cookies) {
				System.out.println(cookie.getName());
				System.out.println(cookie.getValue());
			}
		}		
		
		Date date = new Date();
		// 创建cookie
		Cookie cookie = new Cookie("id", date.getTime()+"");
		
		/*
		 *  设置生命周期
		 *  	0:告知浏览器删除当前同样名字的cookie
		 *  	正数:告知浏览器当前cookie存活多少秒
		 *  	负数:当前会话有效
		 */
		cookie.setMaxAge(-60);
		
		/*
		 * 	设置cookie使用的url格式范围
		 */
		cookie.setPath("/");
		// 向浏览器发送cookie,如果cookie名字相同,那么内容会覆盖掉
		response.addCookie(cookie);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
